//
//  SEMineHeaderView.swift
//  SmartEyeForDoctor
//
//  Created by qdsg on 2021/5/4.
//

import UIKit

class SEMineHeaderView: UIView {

    var tapHandler: (() -> Void)?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setUI()
    }

    func updateData(_ viewModel: SEUserInfoViewModel) {
        avatorImgView.kf.setImage(urlString: viewModel.model.imgPath,
                                  placeholder: viewModel.avatarPlaceholder)
        nameLabel.text = viewModel.model.name
        hospitalLabel.text = viewModel.model.hospitalName
        statusBtn.isHidden = false
        statusBtn.setTitle(viewModel.model.isAuthenticate.title, for: .normal)
    }

    private func setUI() {

        let tap = UITapGestureRecognizer(target: self, action: #selector(tapAction))
        addGestureRecognizer(tap)

        backgroundColor = UIColor(hex: "#45C6A8").withAlphaComponent(0.1)

        addSubview(avatorImgView)
        addSubview(nameLabel)
        addSubview(statusBtn)
        addSubview(hospitalLabel)
        addSubview(arrowImgView)


        avatorImgView.snp.makeConstraints { make in
            make.width.height.equalTo(70)
            make.left.equalTo(15)
            make.bottom.equalTo(-30)
        }

        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(avatorImgView.snp.right).offset(10)
            make.bottom.equalTo(avatorImgView.snp.centerY).offset(-2)
        }

        statusBtn.snp.makeConstraints { make in
            make.left.equalTo(nameLabel.snp.right).offset(10)
            make.centerY.equalTo(nameLabel.snp.centerY)
            make.width.equalTo(52)
            make.height.equalTo(20)
        }

        hospitalLabel.snp.makeConstraints { make in
            make.left.equalTo(avatorImgView.snp.right).offset(10)
            make.top.equalTo(avatorImgView.snp.centerY).offset(6)
            make.right.equalTo(arrowImgView.snp.left).offset(-10)
        }

        arrowImgView.snp.makeConstraints { make in
            make.width.equalTo(12)
            make.height.equalTo(14)
            make.right.equalTo(-15)
            make.centerY.equalTo(avatorImgView.snp.centerY)
        }
    }

    lazy var avatorImgView: UIImageView = {
        let imgView = UIImageView()
        imgView.layer.cornerRadius = imgView.frame.size.height / 2.0
        imgView.layer.masksToBounds = true
        imgView.contentMode = .scaleAspectFill
        return imgView
    }()

    lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.medium(22)
        label.textColor = Asset.textDark.color
        return label
    }()

    lazy var hospitalLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.regular(14)
        label.textColor = Asset.textGray.color
        return label
    }()

    lazy var arrowImgView: UIImageView = {
        let imageView = UIImageView(image: Asset.arrowRightIcon.image)
        return imageView
    }()

    lazy var statusBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.isHidden = true
        btn.setBackgroundImage(Asset.profileStatusBgImage.image, for: .normal)
        btn.setTitleColor(UIColor.white, for: .normal)
        btn.titleLabel?.font = UIFont.regular(12)
        return btn
    }()

    @objc private func tapAction() {
        tapHandler?()
    }

}
